#!/bin/bash

projectname=svcenter
jarname=svcenter-1.0.0
gitlab=https://gitee.com/clematis/monitor.git

function build() {
	newenv=$1
	if [ ! -d "monitor/$projectname" ];then
		git clone $gitlab
		cd monitor/$projectname
	else
		cd monitor/$projectname
		git pull $gitlab
	fi

	oldenv=`cat ./src/main/resources/application.yml | grep "active" | awk '{print $2}'`
	sed -i  "s/$oldenv/$newenv/" ./src/main/resources/application.yml

	mvn clean package
	cp -rf ./target/$jarname.jar ../../
}

function start() {
	ps -ef|grep $jarname |grep -v grep

	if [ $? -eq 0 ];then
		echo "$jarname is running....., please stop first!!!"
	else
		echo "starting....."
		#rm -rf logs/*
		echo '' > nohup.out
		nohup java -jar $jarname.jar &
	fi
}

function status() {
	ps -ef|grep $jarname |grep -v grep

	if [ $? -eq 0 ];then
		echo "$jarname is running....."
	else
		echo "$jarname is stopped."
	fi
}

function stop() {
	ps -ef | grep $jarname | grep -v grep | cut -c 9-15 | xargs sudo kill -s 15
}

if [ "X$1" == "Xbuild" ];then
	if [ "X$2" == "X" ];then
		echo "please input compile env: dev or prod"
	else
		build $2
	fi
elif [ "X$1" == "Xstart" ];then
    start
	sleep 2
	tailf nohup.out
elif [ "X$1" == "Xstop" ];then
    stop
	sleep 2
	status
else
    echo "support command: build start stop status"
    status
fi